/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.gfx;

import inventory.Inventory;
import inventory.Item;
import rpg.Game;
import rpg.Location;
import rpg.Maps.Map;
import rpg.Maps.MapTile;
import rpg.Tiles.RoadTile;
import rpg.entities.Entity;
import rpg.entities.LivingEntity;
import rpg.entities.Player;

/**
 * rothens.tarhely.biz
 * @author Rothens
 */
public class Screen extends Bitmap {

	public class Renderable {

		public MapTile[] tiles;
		public int width;
		public int height;
		public Location topLeft;
		public Location bottomRight;

		public Renderable(Map map, int cX, int cY) {
			topLeft = new Location(cX - 320, cY - 240, false);
			bottomRight = new Location(cX + 319, cY + 239, false);
			width = bottomRight.getTileX() - topLeft.getTileX() + 1;
			height = bottomRight.getTileY() - topLeft.getTileY() + 1;
			getTiles(map);
		}

		private void getTiles(Map map) {
			tiles = new MapTile[width * height];
			int start = topLeft.getMapCoordinate(topLeft, map);
			for (int i = 0; i < width * height; i++) {
				int val = start + i % width + map.getWidth() * (i / width);

				tiles[i] = map.tiles[val];
			}
		}
	}
	private Bitmap viewport;
	int time = 0;
	public int x = 0;
	public int y = 0;

	public Screen(int width, int height) {
		super(width, height);
		viewport = new Bitmap(width, height);
	}

	public void render(Game game) {
		time++;


		if (game.player.getLocation() != null) {

			Location loc = game.player.getLocation();

			//viewport.draw(game.actual.whole, -(loc.getX() - 320) - game.player.xOffs, -(loc.getY() - 240) - game.player.yOffs);
			x = (loc.getX()) + game.player.xOffs;
			y = (loc.getY()) + game.player.yOffs;
			Renderable r = new Renderable(game.actual, x, y);
			for (int i = 0; i < r.width * r.height; i++) {
				int dx = (i % r.width) * 64 - ((x - 320) % 64);
				int dy = (i / r.width) * 64 - ((y - 240) % 64);
				viewport.draw(r.tiles[i].getLandscape().bitmap, dx, dy);
				if(r.tiles[i].getRoad() != null) {
					RoadTile rt = r.tiles[i].getRoad();
					viewport.draw(rt.bitmaps[rt.getState()], dx, dy);
				}
				if (r.tiles[i].getLayer1() != null) {
					viewport.draw(r.tiles[i].getLayer1().bitmap, dx, dy);
				}
				
			}
			
			for(Entity e: game.actual.entities){
				if(e instanceof Player){
					continue;
				}
				if(e.isInView(r)){
					viewport.draw(e.getDrawGraphics(), e.xRenderOffset - 32, e.yRenderOffset - 32);
					if(e instanceof LivingEntity){
						Bar health = ((LivingEntity)e).getHealthBar();
						viewport.draw(health, e.xRenderOffset - 16, e.yRenderOffset -32);
					}
					
				}
			}
			viewport.draw(game.player.getDrawGraphics(), 320 - 32 - game.player.xOffs, 240 - 32 - game.player.yOffs);

			
			
			for (int i = 0; i < r.width * r.height; i++) {
				int dx = (i % r.width) * 64 - ((x - 320) % 64);
				int dy = (i / r.width) * 64 - ((y - 240) % 64);
				if (r.tiles[i].getLayer2() != null) {
					viewport.draw(r.tiles[i].getLayer2().bitmap, dx, dy);
				}
			}
		}
		
		viewport.draw(game.player.energyBar, 10, 460);
		Inventory inv = game.player.getInventory();
		Item itm = inv.getItem("key_red");
		if(itm != null){
			viewport.draw(itm.icon, 610, 410);
		}
		itm = inv.getItem("key_blue");
		if(itm != null){
			viewport.draw(itm.icon, 580, 410);
		}
		itm = inv.getItem("key_green");
		if(itm != null){
			viewport.draw(itm.icon, 550, 410);
		}

		draw(viewport, 0, 0);
	}
}
